package cn.tedu.tmall.admin.mall.service.impl;

import cn.tedu.tmall.admin.mall.dao.persist.repository.IOrderRepository;
import cn.tedu.tmall.admin.mall.pojo.vo.OrderListItemVO;
import cn.tedu.tmall.admin.mall.pojo.vo.OrderStandardVO;
import cn.tedu.tmall.admin.mall.service.IOrderService;
import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.pojo.vo.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderService implements IOrderService {
    @Value("${tmall.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IOrderRepository orderRepository;

    @Override
    public int closeUnpaid() {
        log.debug("开始处理关闭所有超时未支付订单的业务");
        List<Long> idList = orderRepository.listOvertimeUnpaidIdList(15);
        int i=0;
        if (idList.size()>0){
            i = orderRepository.updateStateByBatchIds(idList, ORDER_STATE_CLOSED_BY_SYSTEM);
        }
        log.debug("成功关闭超时未关闭的订单");
        return i;
    }

    @Override
    public OrderStandardVO getStandardById(Long id) {
        log.debug("开始执行根据id查询详情");
        OrderStandardVO standardById = orderRepository.getStandardById(id);
        if (standardById==null){
            String message="查询订单失败,订单数据不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return standardById;
    }

    @Override
    public PageData<OrderListItemVO> list(Integer pageNum) {
        log.debug("开始处理查询订单列表");
        return orderRepository.list(pageNum,defaultQueryPageSize);
    }
}
